
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/polys/wester.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:59 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Examples from Wester’s Article &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="wester.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Polynomials Manipulation Module Reference" href="reference.html" />
    <link rel="prev" title="Basic functionality of the module" href="basics.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="reference.html" title="Polynomials Manipulation Module Reference"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="basics.html" title="Basic functionality of the module"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Polynomial Manipulation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Examples from Wester’s Article</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="examples-from-wester-s-article">
<span id="polys-wester"></span><h1>Examples from Wester’s Article<a class="headerlink" href="#examples-from-wester-s-article" title="Permalink to this headline">¶</a></h1>
<section id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>In this tutorial we present examples from Wester’s article concerning
comparison and critique of mathematical abilities of several computer
algebra systems (see <a class="reference internal" href="#wester1999" id="id1"><span>[Wester1999]</span></a>). All the examples are related to
polynomial and algebraic computations and SymPy specific remarks were
added to all of them.</p>
</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>All examples in this tutorial are computable, so one can just copy and
paste them into a Python shell and do something useful with them. All
computations were done using the following setup:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">init_printing</span><span class="p">(</span><span class="n">use_unicode</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">wrap_line</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="p">(</span><span class="s1">&#39;x,y,z,s,c,n&#39;</span><span class="p">)</span>
<span class="go">(x, y, z, s, c, n)</span>
</pre></div>
</div>
<section id="simple-univariate-polynomial-factorization">
<h3>Simple univariate polynomial factorization<a class="headerlink" href="#simple-univariate-polynomial-factorization" title="Permalink to this headline">¶</a></h3>
<p>To obtain a factorization of a polynomial use <a class="reference internal" href="reference.html#sympy.polys.polytools.factor" title="sympy.polys.polytools.factor"><code class="xref py py-func docutils literal notranslate"><span class="pre">factor()</span></code></a> function.
By default <a class="reference internal" href="reference.html#sympy.polys.polytools.factor" title="sympy.polys.polytools.factor"><code class="xref py py-func docutils literal notranslate"><span class="pre">factor()</span></code></a> returns the result in unevaluated form, so the
content of the input polynomial is left unexpanded, as in the following
example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="mi">6</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">2⋅(3⋅x - 5)</span>
</pre></div>
</div>
<p>To achieve the same effect in a more systematic way use <a class="reference internal" href="reference.html#sympy.polys.polytools.primitive" title="sympy.polys.polytools.primitive"><code class="xref py py-func docutils literal notranslate"><span class="pre">primitive()</span></code></a>
function, which returns the content and the primitive part of the input
polynomial:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">primitive</span><span class="p">(</span><span class="mi">6</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">(2, 3⋅x - 5)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The content and the primitive part can be computed only over a ring. To
simplify coefficients of a polynomial over a field use <a class="reference internal" href="reference.html#sympy.polys.polytools.monic" title="sympy.polys.polytools.monic"><code class="xref py py-func docutils literal notranslate"><span class="pre">monic()</span></code></a>.</p>
</div>
</section>
<section id="univariate-gcd-resultant-and-factorization">
<h3>Univariate GCD, resultant and factorization<a class="headerlink" href="#univariate-gcd-resultant-and-factorization" title="Permalink to this headline">¶</a></h3>
<p>Consider univariate polynomials <code class="docutils literal notranslate"><span class="pre">f</span></code>, <code class="docutils literal notranslate"><span class="pre">g</span></code> and <code class="docutils literal notranslate"><span class="pre">h</span></code> over integers:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="mi">64</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">34</span> <span class="o">-</span> <span class="mi">21</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">47</span> <span class="o">-</span> <span class="mi">126</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">8</span> <span class="o">-</span> <span class="mi">46</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">5</span> <span class="o">-</span> <span class="mi">16</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">60</span> <span class="o">-</span> <span class="mi">81</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="mi">72</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">60</span> <span class="o">-</span> <span class="mi">25</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">25</span> <span class="o">-</span> <span class="mi">19</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">23</span> <span class="o">-</span> <span class="mi">22</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">39</span> <span class="o">-</span> <span class="mi">83</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">52</span> <span class="o">+</span> <span class="mi">54</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">10</span> <span class="o">+</span> <span class="mi">81</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="mi">34</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">19</span> <span class="o">-</span> <span class="mi">25</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">16</span> <span class="o">+</span> <span class="mi">70</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">7</span> <span class="o">+</span> <span class="mi">20</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">-</span> <span class="mi">91</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">86</span>
</pre></div>
</div>
<p>We can compute the greatest common divisor (GCD) of two polynomials using
<a class="reference internal" href="reference.html#sympy.polys.polytools.gcd" title="sympy.polys.polytools.gcd"><code class="xref py py-func docutils literal notranslate"><span class="pre">gcd()</span></code></a> function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gcd</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">g</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<p>We see that <code class="docutils literal notranslate"><span class="pre">f</span></code> and <code class="docutils literal notranslate"><span class="pre">g</span></code> have no common factors. However, <code class="docutils literal notranslate"><span class="pre">f*h</span></code> and <code class="docutils literal notranslate"><span class="pre">g*h</span></code>
have an obvious factor <code class="docutils literal notranslate"><span class="pre">h</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gcd</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">f</span><span class="o">*</span><span class="n">h</span><span class="p">),</span> <span class="n">expand</span><span class="p">(</span><span class="n">g</span><span class="o">*</span><span class="n">h</span><span class="p">))</span> <span class="o">-</span> <span class="n">h</span>
<span class="go">0</span>
</pre></div>
</div>
<p>The same can be verified using the resultant of univariate polynomials:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">resultant</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">f</span><span class="o">*</span><span class="n">h</span><span class="p">),</span> <span class="n">expand</span><span class="p">(</span><span class="n">g</span><span class="o">*</span><span class="n">h</span><span class="p">))</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Factorization of large univariate polynomials (of degree 120 in this case) over
integers is also possible:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">f</span><span class="o">*</span><span class="n">g</span><span class="p">))</span>
<span class="go"> ⎛    60       47       34        8       5     ⎞ ⎛    60       52     39       25       23       10     ⎞</span>
<span class="go">-⎝16⋅x   + 21⋅x   - 64⋅x   + 126⋅x  + 46⋅x  + 81⎠⋅⎝72⋅x   - 83⋅x - 22⋅x   - 25⋅x   - 19⋅x   + 54⋅x   + 81⎠</span>
</pre></div>
</div>
</section>
<section id="multivariate-gcd-and-factorization">
<h3>Multivariate GCD and factorization<a class="headerlink" href="#multivariate-gcd-and-factorization" title="Permalink to this headline">¶</a></h3>
<p>What can be done in univariate case, can be also done for multivariate
polynomials. Consider the following polynomials <code class="docutils literal notranslate"><span class="pre">f</span></code>, <code class="docutils literal notranslate"><span class="pre">g</span></code> and <code class="docutils literal notranslate"><span class="pre">h</span></code>
in <span class="math notranslate nohighlight">\(\mathbb{Z}[x,y,z]\)</span>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="mi">24</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">19</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">8</span> <span class="o">-</span> <span class="mi">47</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">17</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">5</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">8</span> <span class="o">+</span> <span class="mi">6</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">15</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">9</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">22</span> <span class="o">+</span> <span class="mi">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="mi">34</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">5</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">8</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">13</span> <span class="o">+</span> <span class="mi">20</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">7</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">7</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">7</span> <span class="o">+</span> <span class="mi">12</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">9</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">16</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="mi">80</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">14</span><span class="o">*</span><span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="mi">11</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">12</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">7</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">13</span> <span class="o">-</span> <span class="mi">23</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">8</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">10</span> <span class="o">+</span> <span class="mi">47</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">17</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">5</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">8</span>
</pre></div>
</div>
<p>As previously, we can verify that <code class="docutils literal notranslate"><span class="pre">f</span></code> and <code class="docutils literal notranslate"><span class="pre">g</span></code> have no common factors:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gcd</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">g</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<p>However, <code class="docutils literal notranslate"><span class="pre">f*h</span></code> and <code class="docutils literal notranslate"><span class="pre">g*h</span></code> have an obvious factor <code class="docutils literal notranslate"><span class="pre">h</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gcd</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">f</span><span class="o">*</span><span class="n">h</span><span class="p">),</span> <span class="n">expand</span><span class="p">(</span><span class="n">g</span><span class="o">*</span><span class="n">h</span><span class="p">))</span> <span class="o">-</span> <span class="n">h</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Multivariate factorization of large polynomials is also possible:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">f</span><span class="o">*</span><span class="n">g</span><span class="p">))</span>
<span class="go">    7   ⎛   9  9  3       7  6       5    12       7⎞ ⎛   22       17  5  8      15  9  2         19  8    ⎞</span>
<span class="go">-2⋅y ⋅z⋅⎝6⋅x ⋅y ⋅z  + 10⋅x ⋅z  + 17⋅x ⋅y⋅z   + 40⋅y ⎠⋅⎝3⋅x   + 47⋅x  ⋅y ⋅z  - 6⋅x  ⋅y ⋅z  - 24⋅x⋅y  ⋅z  - 5⎠</span>
</pre></div>
</div>
</section>
<section id="support-for-symbols-in-exponents">
<h3>Support for symbols in exponents<a class="headerlink" href="#support-for-symbols-in-exponents" title="Permalink to this headline">¶</a></h3>
<p>Polynomial manipulation functions provided by <a class="reference internal" href="reference.html#module-sympy.polys" title="sympy.polys"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.polys</span></code></a> are mostly
used with integer exponents. However, it’s perfectly valid to compute with
symbolic exponents, e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gcd</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">4</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">2</span><span class="p">),</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="p">)</span>
<span class="go"> n</span>
<span class="go">x</span>
</pre></div>
</div>
</section>
<section id="testing-if-polynomials-have-common-zeros">
<h3>Testing if polynomials have common zeros<a class="headerlink" href="#testing-if-polynomials-have-common-zeros" title="Permalink to this headline">¶</a></h3>
<p>To test if two polynomials have a root in common we can use <a class="reference internal" href="reference.html#sympy.polys.polytools.resultant" title="sympy.polys.polytools.resultant"><code class="xref py py-func docutils literal notranslate"><span class="pre">resultant()</span></code></a>
function. The theory says that the resultant of two polynomials vanishes if
there is a common zero of those polynomials. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">resultant</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<p>We can visualize this fact by factoring the polynomials:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">        ⎛   3        ⎞</span>
<span class="go">(x + 1)⋅⎝3⋅x  + x - 2⎠</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">        ⎛ 2          ⎞</span>
<span class="go">(x + 1)⋅⎝x  - 4⋅x + 5⎠</span>
</pre></div>
</div>
<p>In both cases we obtained the factor <span class="math notranslate nohighlight">\(x + 1\)</span> which tells us that the common
root is <span class="math notranslate nohighlight">\(x = -1\)</span>.</p>
</section>
<section id="normalizing-simple-rational-functions">
<h3>Normalizing simple rational functions<a class="headerlink" href="#normalizing-simple-rational-functions" title="Permalink to this headline">¶</a></h3>
<p>To remove common factors from the numerator and the denominator of a rational
function the elegant way, use <a class="reference internal" href="reference.html#sympy.polys.polytools.cancel" title="sympy.polys.polytools.cancel"><code class="xref py py-func docutils literal notranslate"><span class="pre">cancel()</span></code></a> function. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="p">))</span>
<span class="go">x - 2</span>
<span class="go">─────</span>
<span class="go">x + 2</span>
</pre></div>
</div>
</section>
<section id="expanding-expressions-and-factoring-back">
<h3>Expanding expressions and factoring back<a class="headerlink" href="#expanding-expressions-and-factoring-back" title="Permalink to this headline">¶</a></h3>
<p>One can work easily we expressions in both expanded and factored forms.
Consider a polynomial <code class="docutils literal notranslate"><span class="pre">f</span></code> in expanded form. We differentiate it and
factor the result back:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">expand</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">20</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">diff</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">          19</span>
<span class="go">20⋅(x + 1)</span>
</pre></div>
</div>
<p>The same can be achieved in factored form:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">20</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="go">          19</span>
<span class="go">20⋅(x + 1)</span>
</pre></div>
</div>
</section>
<section id="factoring-in-terms-of-cyclotomic-polynomials">
<h3>Factoring in terms of cyclotomic polynomials<a class="headerlink" href="#factoring-in-terms-of-cyclotomic-polynomials" title="Permalink to this headline">¶</a></h3>
<p>SymPy can very efficiently decompose polynomials of the form <span class="math notranslate nohighlight">\(x^n \pm 1\)</span> in
terms of cyclotomic polynomials:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">15</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">        ⎛ 2        ⎞ ⎛ 4    3    2        ⎞ ⎛ 8    7    5    4    3       ⎞</span>
<span class="go">(x - 1)⋅⎝x  + x + 1⎠⋅⎝x  + x  + x  + x + 1⎠⋅⎝x  - x  + x  - x  + x - x + 1⎠</span>
</pre></div>
</div>
<p>The original Wester`s example was <span class="math notranslate nohighlight">\(x^{100} - 1\)</span>, but was truncated for
readability purpose. Note that this is not a big struggle for <a class="reference internal" href="reference.html#sympy.polys.polytools.factor" title="sympy.polys.polytools.factor"><code class="xref py py-func docutils literal notranslate"><span class="pre">factor()</span></code></a>
to decompose polynomials of degree 1000 or greater.</p>
</section>
<section id="univariate-factoring-over-gaussian-numbers">
<h3>Univariate factoring over Gaussian numbers<a class="headerlink" href="#univariate-factoring-over-gaussian-numbers" title="Permalink to this headline">¶</a></h3>
<p>Consider a univariate polynomial <code class="docutils literal notranslate"><span class="pre">f</span></code> with integer coefficients:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="mi">8</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">77</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">18</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">153</span>
</pre></div>
</div>
<p>We want to obtain a factorization of <code class="docutils literal notranslate"><span class="pre">f</span></code> over Gaussian numbers. To do this
we use <a class="reference internal" href="reference.html#sympy.polys.polytools.factor" title="sympy.polys.polytools.factor"><code class="xref py py-func docutils literal notranslate"><span class="pre">factor()</span></code></a> as previously, but this time we set <code class="docutils literal notranslate"><span class="pre">gaussian</span></code> keyword
to <code class="docutils literal notranslate"><span class="pre">True</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">gaussian</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">  ⎛    3⋅ⅈ⎞ ⎛    3⋅ⅈ⎞</span>
<span class="go">4⋅⎜x - ───⎟⋅⎜x + ───⎟⋅(x + 1 - 4⋅ⅈ)⋅(x + 1 + 4⋅ⅈ)</span>
<span class="go">  ⎝     2 ⎠ ⎝     2 ⎠</span>
</pre></div>
</div>
<p>As the result we got a splitting factorization of <code class="docutils literal notranslate"><span class="pre">f</span></code> with monic factors
(this is a general rule when computing in a field with SymPy). The <code class="docutils literal notranslate"><span class="pre">gaussian</span></code>
keyword is useful for improving code readability, however the same result can
be computed using more general syntax:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="n">I</span><span class="p">)</span>
<span class="go">  ⎛    3⋅ⅈ⎞ ⎛    3⋅ⅈ⎞</span>
<span class="go">4⋅⎜x - ───⎟⋅⎜x + ───⎟⋅(x + 1 - 4⋅ⅈ)⋅(x + 1 + 4⋅ⅈ)</span>
<span class="go">  ⎝     2 ⎠ ⎝     2 ⎠</span>
</pre></div>
</div>
</section>
<section id="computing-with-automatic-field-extensions">
<h3>Computing with automatic field extensions<a class="headerlink" href="#computing-with-automatic-field-extensions" title="Permalink to this headline">¶</a></h3>
<p>Consider two univariate polynomials <code class="docutils literal notranslate"><span class="pre">f</span></code> and <code class="docutils literal notranslate"><span class="pre">g</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span>
</pre></div>
</div>
<p>We would like to reduce degrees of the numerator and the denominator of a
rational function <code class="docutils literal notranslate"><span class="pre">f/g</span></code>. To do this we employ <a class="reference internal" href="reference.html#sympy.polys.polytools.cancel" title="sympy.polys.polytools.cancel"><code class="xref py py-func docutils literal notranslate"><span class="pre">cancel()</span></code></a> function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">(</span><span class="n">f</span><span class="o">/</span><span class="n">g</span><span class="p">)</span>
<span class="go"> 3      2       2</span>
<span class="go">x  - 2⋅x  + √2⋅x  - 3⋅x - 2⋅√2⋅x - 3⋅√2</span>
<span class="go">───────────────────────────────────────</span>
<span class="go">                  2</span>
<span class="go">                 x  - 2</span>
</pre></div>
</div>
<p>Unfortunately nothing interesting happened. This is because by default SymPy
treats <span class="math notranslate nohighlight">\(\sqrt{2}\)</span> as a generator, obtaining a bivariate polynomial for the
numerator. To make <a class="reference internal" href="reference.html#sympy.polys.polytools.cancel" title="sympy.polys.polytools.cancel"><code class="xref py py-func docutils literal notranslate"><span class="pre">cancel()</span></code></a> recognize algebraic properties of <span class="math notranslate nohighlight">\(\sqrt{2}\)</span>,
one needs to use <code class="docutils literal notranslate"><span class="pre">extension</span></code> keyword:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">(</span><span class="n">f</span><span class="o">/</span><span class="n">g</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go"> 2</span>
<span class="go">x  - 2⋅x - 3</span>
<span class="go">────────────</span>
<span class="go">   x - √2</span>
</pre></div>
</div>
<p>Setting <code class="docutils literal notranslate"><span class="pre">extension=True</span></code> tells <a class="reference internal" href="reference.html#sympy.polys.polytools.cancel" title="sympy.polys.polytools.cancel"><code class="xref py py-func docutils literal notranslate"><span class="pre">cancel()</span></code></a> to find minimal algebraic
number domain for the coefficients of <code class="docutils literal notranslate"><span class="pre">f/g</span></code>. The automatically inferred
domain is <span class="math notranslate nohighlight">\(\mathbb{Q}(\sqrt{2})\)</span>. If one doesn’t want to rely on automatic
inference, the same result can be obtained by setting the <code class="docutils literal notranslate"><span class="pre">extension</span></code>
keyword with an explicit algebraic number:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">(</span><span class="n">f</span><span class="o">/</span><span class="n">g</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="go"> 2</span>
<span class="go">x  - 2⋅x - 3</span>
<span class="go">────────────</span>
<span class="go">   x - √2</span>
</pre></div>
</div>
</section>
<section id="univariate-factoring-over-various-domains">
<h3>Univariate factoring over various domains<a class="headerlink" href="#univariate-factoring-over-various-domains" title="Permalink to this headline">¶</a></h3>
<p>Consider a univariate polynomial <code class="docutils literal notranslate"><span class="pre">f</span></code> with integer coefficients:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">4</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span>
</pre></div>
</div>
<p>With <a class="reference internal" href="reference.html#module-sympy.polys" title="sympy.polys"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.polys</span></code></a> we can obtain factorizations of <code class="docutils literal notranslate"><span class="pre">f</span></code> over different
domains, which includes:</p>
<ul>
<li><p>rationals:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">⎛ 2        ⎞ ⎛ 2        ⎞</span>
<span class="go">⎝x  - x - 1⎠⋅⎝x  + x - 1⎠</span>
</pre></div>
</div>
</li>
<li><p>finite fields:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">modulus</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="go">       2        2</span>
<span class="go">(x - 2) ⋅(x + 2)</span>
</pre></div>
</div>
</li>
<li><p>algebraic numbers:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">alg</span> <span class="o">=</span> <span class="n">AlgebraicNumber</span><span class="p">((</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">alias</span><span class="o">=</span><span class="s1">&#39;alpha&#39;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="n">alg</span><span class="p">)</span>
<span class="go">(x - α)⋅(x + α)⋅(x - 1 - α)⋅(x + α + 1)</span>
</pre></div>
</div>
</li>
</ul>
</section>
<section id="factoring-polynomials-into-linear-factors">
<h3>Factoring polynomials into linear factors<a class="headerlink" href="#factoring-polynomials-into-linear-factors" title="Permalink to this headline">¶</a></h3>
<p>Currently SymPy can factor polynomials into irreducibles over various domains,
which can result in a splitting factorization (into linear factors). However,
there is currently no systematic way to infer a splitting field (algebraic
number field) automatically. In future the following syntax will be
implemented:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">7</span><span class="p">,</span> <span class="n">split</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">NotImplementedError</span>: <span class="n">&#39;split&#39; option is not implemented yet</span>
</pre></div>
</div>
<p>Note this is different from <code class="docutils literal notranslate"><span class="pre">extension=True</span></code>, because the later only tells how
expression parsing should be done, not what should be the domain of computation.
One can simulate the <code class="docutils literal notranslate"><span class="pre">split</span></code> keyword for several classes of polynomials using
<a class="reference internal" href="../solvers/solvers.html#sympy.solvers.solvers.solve" title="sympy.solvers.solvers.solve"><code class="xref py py-func docutils literal notranslate"><span class="pre">solve()</span></code></a> function.</p>
</section>
<section id="advanced-factoring-over-finite-fields">
<h3>Advanced factoring over finite fields<a class="headerlink" href="#advanced-factoring-over-finite-fields" title="Permalink to this headline">¶</a></h3>
<p>Consider a univariate polynomial <code class="docutils literal notranslate"><span class="pre">f</span></code> with integer coefficients:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">11</span> <span class="o">+</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
</pre></div>
</div>
<p>We can factor <code class="docutils literal notranslate"><span class="pre">f</span></code> over a large finite field <span class="math notranslate nohighlight">\(F_{65537}\)</span>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">modulus</span><span class="o">=</span><span class="mi">65537</span><span class="p">)</span>
<span class="go">⎛ 2        ⎞ ⎛ 9    8    6    5    3    2    ⎞</span>
<span class="go">⎝x  + x + 1⎠⋅⎝x  - x  + x  - x  + x  - x  + 1⎠</span>
</pre></div>
</div>
<p>and expand the resulting factorization back:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go"> 11</span>
<span class="go">x   + x + 1</span>
</pre></div>
</div>
<p>obtaining polynomial <code class="docutils literal notranslate"><span class="pre">f</span></code>. This was done using symmetric polynomial
representation over finite fields The same thing can be done using
non-symmetric representation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">modulus</span><span class="o">=</span><span class="mi">65537</span><span class="p">,</span> <span class="n">symmetric</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">⎛ 2        ⎞ ⎛ 9          8    6          5    3          2    ⎞</span>
<span class="go">⎝x  + x + 1⎠⋅⎝x  + 65536⋅x  + x  + 65536⋅x  + x  + 65536⋅x  + 1⎠</span>
</pre></div>
</div>
<p>As with symmetric representation we can expand the factorization
to get the input polynomial back. This time, however, we need to
truncate coefficients of the expanded polynomial modulo 65537:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">trunc</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">_</span><span class="p">),</span> <span class="mi">65537</span><span class="p">)</span>
<span class="go"> 11</span>
<span class="go">x   + x + 1</span>
</pre></div>
</div>
</section>
<section id="working-with-expressions-as-polynomials">
<h3>Working with expressions as polynomials<a class="headerlink" href="#working-with-expressions-as-polynomials" title="Permalink to this headline">¶</a></h3>
<p>Consider a multivariate polynomial <code class="docutils literal notranslate"><span class="pre">f</span></code> in <span class="math notranslate nohighlight">\(\mathbb{Z}[x,y,z]\)</span>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">expand</span><span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="mi">20</span><span class="p">)</span>
</pre></div>
</div>
<p>We want to compute factorization of <code class="docutils literal notranslate"><span class="pre">f</span></code>. To do this we use <code class="docutils literal notranslate"><span class="pre">factor</span></code> as
usually, however we note that the polynomial in consideration is already
in expanded form, so we can tell the factorization routine to skip
expanding <code class="docutils literal notranslate"><span class="pre">f</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">expand</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">                 20</span>
<span class="go">⎛       2      3⎞</span>
<span class="go">⎝x - 2⋅y  + 3⋅z ⎠</span>
</pre></div>
</div>
<p>The default in <a class="reference internal" href="reference.html#module-sympy.polys" title="sympy.polys"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.polys</span></code></a> is to expand all expressions given as
arguments to polynomial manipulation functions and <a class="reference internal" href="reference.html#sympy.polys.polytools.Poly" title="sympy.polys.polytools.Poly"><code class="xref py py-class docutils literal notranslate"><span class="pre">Poly</span></code></a> class.
If we know that expanding is unnecessary, then by setting <code class="docutils literal notranslate"><span class="pre">expand=False</span></code>
we can save quite a lot of time for complicated inputs. This can be really
important when computing with expressions like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">expand</span><span class="p">((</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">tan</span><span class="p">(</span><span class="n">z</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="mi">20</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">expand</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">                                 20</span>
<span class="go">⎛               2           3   ⎞</span>
<span class="go">⎝-sin(x) + 2⋅cos (y) - 3⋅tan (z)⎠</span>
</pre></div>
</div>
</section>
<section id="computing-reduced-grobner-bases">
<h3>Computing reduced Gröbner bases<a class="headerlink" href="#computing-reduced-grobner-bases" title="Permalink to this headline">¶</a></h3>
<p>To compute a reduced Gröbner basis for a set of polynomials use the
<a class="reference internal" href="reference.html#sympy.polys.polytools.groebner" title="sympy.polys.polytools.groebner"><code class="xref py py-func docutils literal notranslate"><span class="pre">groebner()</span></code></a> function. The function accepts various
monomial orderings, e.g.: <code class="docutils literal notranslate"><span class="pre">lex</span></code>, <code class="docutils literal notranslate"><span class="pre">grlex</span></code> and <code class="docutils literal notranslate"><span class="pre">grevlex</span></code>, or a user
defined one, via <code class="docutils literal notranslate"><span class="pre">order</span></code> keyword. The <code class="docutils literal notranslate"><span class="pre">lex</span></code> ordering is the most
interesting because it has elimination property, which means that if the
system of polynomial equations to <a class="reference internal" href="reference.html#sympy.polys.polytools.groebner" title="sympy.polys.polytools.groebner"><code class="xref py py-func docutils literal notranslate"><span class="pre">groebner()</span></code></a> is
zero-dimensional (has finite number of solutions) the last element of the
basis is a univariate polynomial. Consider the following example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">expand</span><span class="p">((</span><span class="mi">1</span> <span class="o">-</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">5</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">s</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">5</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">s</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">10</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">groebner</span><span class="p">([</span><span class="n">f</span><span class="p">,</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">s</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>
<span class="go">             ⎛⎡ 2    2       20      18       16       14      12    10⎤                           ⎞</span>
<span class="go">GroebnerBasis⎝⎣c  + s  - 1, c   - 5⋅c   + 10⋅c   - 10⋅c   + 5⋅c   - c  ⎦, s, c, domain=ℤ, order=lex⎠</span>
</pre></div>
</div>
<p>The result is an ordinary Python list, so we can easily apply a function to
all its elements, for example we can factor those elements:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">factor</span><span class="p">,</span> <span class="n">_</span><span class="p">))</span>
<span class="go">⎡ 2    2       10        5        5⎤</span>
<span class="go">⎣c  + s  - 1, c  ⋅(c - 1) ⋅(c + 1) ⎦</span>
</pre></div>
</div>
<p>From the above we can easily find all solutions of the system of polynomial
equations. Or we can use <a class="reference internal" href="../solvers/solvers.html#sympy.solvers.solvers.solve" title="sympy.solvers.solvers.solve"><code class="xref py py-func docutils literal notranslate"><span class="pre">solve()</span></code></a> to achieve this in a more systematic
way:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">solve</span><span class="p">([</span><span class="n">f</span><span class="p">,</span> <span class="n">s</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">c</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
<span class="go">[(-1, 0), (0, -1), (0, 1), (1, 0)]</span>
</pre></div>
</div>
</section>
<section id="multivariate-factoring-over-algebraic-numbers">
<h3>Multivariate factoring over algebraic numbers<a class="headerlink" href="#multivariate-factoring-over-algebraic-numbers" title="Permalink to this headline">¶</a></h3>
<p>Computing with multivariate polynomials over various domains is as simple as
in univariate case. For example consider the following factorization over
<span class="math notranslate nohighlight">\(\mathbb{Q}(\sqrt{-3})\)</span>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">3</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">))</span>
<span class="go">        ⎛      ⎛  1   √3⋅ⅈ⎞⎞ ⎛      ⎛  1   √3⋅ⅈ⎞⎞</span>
<span class="go">(x + y)⋅⎜x + y⋅⎜- ─ - ────⎟⎟⋅⎜x + y⋅⎜- ─ + ────⎟⎟</span>
<span class="go">        ⎝      ⎝  2    2  ⎠⎠ ⎝      ⎝  2    2  ⎠⎠</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Currently multivariate polynomials over finite fields aren’t supported.</p>
</div>
</section>
<section id="partial-fraction-decomposition">
<h3>Partial fraction decomposition<a class="headerlink" href="#partial-fraction-decomposition" title="Permalink to this headline">¶</a></h3>
<p>Consider a univariate rational function <code class="docutils literal notranslate"><span class="pre">f</span></code> with integer coefficients:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>To decompose <code class="docutils literal notranslate"><span class="pre">f</span></code> into partial fractions use <a class="reference internal" href="reference.html#sympy.polys.partfrac.apart" title="sympy.polys.partfrac.apart"><code class="xref py py-func docutils literal notranslate"><span class="pre">apart()</span></code></a> function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">apart</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">  3       2        2</span>
<span class="go">───── - ───── + ────────</span>
<span class="go">x + 2   x + 1          2</span>
<span class="go">                (x + 1)</span>
</pre></div>
</div>
<p>To return from partial fractions to the rational function use
a composition of <a class="reference internal" href="reference.html#sympy.polys.rationaltools.together" title="sympy.polys.rationaltools.together"><code class="xref py py-func docutils literal notranslate"><span class="pre">together()</span></code></a> and <a class="reference internal" href="reference.html#sympy.polys.polytools.cancel" title="sympy.polys.polytools.cancel"><code class="xref py py-func docutils literal notranslate"><span class="pre">cancel()</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">(</span><span class="n">together</span><span class="p">(</span><span class="n">_</span><span class="p">))</span>
<span class="go">     2</span>
<span class="go">    x  + 2⋅x + 3</span>
<span class="go">───────────────────</span>
<span class="go"> 3      2</span>
<span class="go">x  + 4⋅x  + 5⋅x + 2</span>
</pre></div>
</div>
</section>
</section>
<section id="literature">
<h2>Literature<a class="headerlink" href="#literature" title="Permalink to this headline">¶</a></h2>
<dl class="citation">
<dt class="label" id="wester1999"><span class="brackets"><a class="fn-backref" href="#id1">Wester1999</a></span></dt>
<dd><p>Michael J. Wester, A Critique of the Mathematical Abilities of
CA Systems, 1999, <a class="reference external" href="http://www.math.unm.edu/~wester/cas/book/Wester.pdf">http://www.math.unm.edu/~wester/cas/book/Wester.pdf</a></p>
</dd>
</dl>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Examples from Wester’s Article</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#examples">Examples</a><ul>
<li><a class="reference internal" href="#simple-univariate-polynomial-factorization">Simple univariate polynomial factorization</a></li>
<li><a class="reference internal" href="#univariate-gcd-resultant-and-factorization">Univariate GCD, resultant and factorization</a></li>
<li><a class="reference internal" href="#multivariate-gcd-and-factorization">Multivariate GCD and factorization</a></li>
<li><a class="reference internal" href="#support-for-symbols-in-exponents">Support for symbols in exponents</a></li>
<li><a class="reference internal" href="#testing-if-polynomials-have-common-zeros">Testing if polynomials have common zeros</a></li>
<li><a class="reference internal" href="#normalizing-simple-rational-functions">Normalizing simple rational functions</a></li>
<li><a class="reference internal" href="#expanding-expressions-and-factoring-back">Expanding expressions and factoring back</a></li>
<li><a class="reference internal" href="#factoring-in-terms-of-cyclotomic-polynomials">Factoring in terms of cyclotomic polynomials</a></li>
<li><a class="reference internal" href="#univariate-factoring-over-gaussian-numbers">Univariate factoring over Gaussian numbers</a></li>
<li><a class="reference internal" href="#computing-with-automatic-field-extensions">Computing with automatic field extensions</a></li>
<li><a class="reference internal" href="#univariate-factoring-over-various-domains">Univariate factoring over various domains</a></li>
<li><a class="reference internal" href="#factoring-polynomials-into-linear-factors">Factoring polynomials into linear factors</a></li>
<li><a class="reference internal" href="#advanced-factoring-over-finite-fields">Advanced factoring over finite fields</a></li>
<li><a class="reference internal" href="#working-with-expressions-as-polynomials">Working with expressions as polynomials</a></li>
<li><a class="reference internal" href="#computing-reduced-grobner-bases">Computing reduced Gröbner bases</a></li>
<li><a class="reference internal" href="#multivariate-factoring-over-algebraic-numbers">Multivariate factoring over algebraic numbers</a></li>
<li><a class="reference internal" href="#partial-fraction-decomposition">Partial fraction decomposition</a></li>
</ul>
</li>
<li><a class="reference internal" href="#literature">Literature</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="basics.html"
                        title="previous chapter">Basic functionality of the module</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="reference.html"
                        title="next chapter">Polynomials Manipulation Module Reference</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/polys/wester.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="reference.html" title="Polynomials Manipulation Module Reference"
             >next</a> |</li>
        <li class="right" >
          <a href="basics.html" title="Basic functionality of the module"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Polynomial Manipulation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Examples from Wester’s Article</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/polys/wester.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:59 GMT -->
</html>